"""
@author: wg
@file: homework12_4_1.py
@time: 2023/12/4 16:01
"""

# 1、下载sc官网图片

from bs4 import BeautifulSoup
import os
from download import downloadtext, downloadcontent

for i in range(1):
    path = "sc-picture"
    # 如果目录不存在，则自动创建目录
    if not os.path.isdir(path):
        os.mkdir(path)
    url = "https://www.sanchuangedu.cn/"
    text = downloadtext(url)
    # 提取需要的数据
    soup = BeautifulSoup(text, "lxml")
    # 总共25个
    all_picture = soup.select("img")
    for picture in all_picture:
        scpicture = picture["src"]
        # img_src = url + scpicture
        # print(img_src)
        filename = f"{path}/{scpicture}"
        with open(filename, "wb") as f:
            # 访问封面图片
            f.write(downloadcontent(scpicture))


